<template>
  <el-card>
    <el-button
      @click="$router.push('/menu/add')"
      icon="el-icon-circle-plus-outline"
      type="primary"
    >菜单添加</el-button>
    <el-divider></el-divider>
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID" sortable width="180"></el-table-column>
      <el-table-column prop="title" label="名称" sortable width="180"></el-table-column>
      <el-table-column prop="type" label="类型">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.type==1">目录</el-tag>
          <el-tag type="warning" v-else>链接</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标">
        <template v-slot="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="地址"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">启用</el-tag>
          <el-tag type="error" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="280" label="操作" fixed="right">
        <template v-slot="scope">
          <el-button type="success" @click="edit(scope.row.id)" size="mini">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: []
    };
  },
  methods: {
    edit(id) {
      // 跳转到菜单编辑页面
      this.$router.push("/menu/" + id);
    },
    del(id) {
      this.$myConfirm(()=>{
        this.$http.post("/api/menudelete", { id }).then(res => {
          if (res.code == 200) {
            this.list = res.list;
          } else {
            this.$message({
              type: "error",
              message: "删除失败"
            });
          }
        });
      });
    },
    getMenu() {
      this.$http.get("/api/menulist?istree=1").then(res => {
        if (res.code == 200) {
          this.list = res.list;
        }
      });
    }
  },
  created() {
    this.getMenu();
  }
};
</script>

<style>
</style>